<?php

namespace Admin\Controller;

use User\Api\UserApi;

//门店管理

class StoreController extends AdminController {

    private $m;

    public function _initialize() {
        parent::_initialize();
        //品牌
        $brand = M("brand")->select();
        $this->assign("brand", $brand);
        $this->assign("meta_title", "门店管理");
        $this->m = D("Store");
    }

    public function index() {
        $where = array();
        $keyword = I("keyword", '', 'strip_tags');
        if (!empty($keyword)) {
            $where["sname"] = array("like", '%' . $keyword . '%');
        }
         C('LIST_ROWS',12);
        $list = $this->lists($this->m->relation("brand"), $where);
        foreach ($list as &$item) {
            $item['pic'] = explode(',', $item['pic']);
        }
        $this->assign("list", $list);
        $this->display();
    }

    public function add() {
        if (IS_POST) {
            $_POST['pic'] = implode(',', $_POST['pic']);
            $username = $_POST["sname"];
            /* 调用注册接口注册用户 */
            $User = new UserApi;
            $uid = $User->register($username, '123456', time() . "@qq.com");
            if (0 < $uid) { //注册成功
                $user = array('uid' => $uid, 'nickname' => $username, 'status' => 1);
                if (!M('Member')->add($user)) {
                    $this->error('门店添加失败！');
                } else {
                    //添加门店
                    if ($_POST["location"] != null) {
                        $location = str_replace("，", ",", I("location"));
                        $location = explode(",", $_POST["location"]);
                        $_POST["lon"] = $location[0];
                        $_POST["lat"] = $location[1];
                    }

                    $_POST["uid"] = $uid;
                    $this->m->create(I("post."));
                    $result = $this->m->add();
                    if ($result) {
                        $this->success("操作成功！", U('index'));
                    } else {
                        $this->error("门店添加发生错误！" . $this->m->getError());
                    }
                }
            } else { //注册失败，显示错误信息
                $this->error($this->showRegError($uid));
            }
        } else {
            $this->display();
        }
    }

    public function edit($id) {
        if (IS_POST) {
            $_POST['pic'] = implode(',', $_POST['pic']);
            //添加门店
            if ($_POST["location"] != null) {
                $location = str_replace("，", ",", I("location"));
                $location = explode(",", $location);
                $_POST["lon"] = $location[0];
                $_POST["lat"] = $location[1];
            }
            $modle = new \Think\Model;
            $modle->startTrans();
            $where["id"] = array("eq", I("id"));
            $faxiu_store = array(
                "sname" => I("sname"),
                "pic" => I("pic"),
                "address" => I("address"),
                "phone" => I("phone"),
                "bid" => I("bid"),
                "explain" => I("explain"),
                "sort" => I("sort"),
                "lon" => I("lon"),
                "lat" => I("lat")
            );
            $result = $modle->table("faxiu_store")->where($where)->save($faxiu_store);
            //分别编辑两个表的名称
            $faxiu_member = array("nickname" => I('sname'));
            $where1["uid"] = array("eq", I("uid"));
            $result1 = $modle->table("faxiu_member")->where($where1)->save($faxiu_member);
            $faxiu_ucenter_member = array("username" => I('sname'));
            $where2["id"] = array("eq", I("uid"));
            $result2 = $modle->table("faxiu_ucenter_member")->where($where2)->save($faxiu_ucenter_member);
            if ($result !== false && $result1 !== false && $result2 !== false) {
                $modle->commit();
                $this->success("操作成功！", U('index'));
            } else {
                $modle->rollback();
                $this->error(M()->getDbError());
            }
        } else {
            $info = $this->m->find($id);
            if ($info["lon"] && $info["lat"]) {
                $info["location"] = $info["lon"] . "," . $info["lat"];
            }
            $info['pic'] = explode(',', $info['pic']);
            $this->assign("info", $info);
            $this->display("add");
        }
    }

    public function del() {
        $id = array_unique((array) I('id', 0));

        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }

        $map = array('id' => array('in', $id));
        if ($this->m->where($map)->delete()) {
            // S('DB_CONFIG_DATA',null);
            //记录行为
            action_log('delete_store', 'Store', $id, UID);
            $this->success('删除成功');
        } else {
            $this->error('删除失败！');
        }
    }

}
